package com.example.demo.mapper;

import com.example.demo.enumeration.Campus;
import com.example.demo.enumeration.ItemCategory;
import com.example.demo.enumeration.ItemStatus;
import com.example.demo.enumeration.SortOrder;
import com.example.demo.pojo.entity.Item;
import com.example.demo.pojo.vo.ItemListVO;
import com.github.pagehelper.Page;
import org.apache.ibatis.annotations.*;

import java.math.BigDecimal;
import java.util.List;

@Mapper
public interface ItemMapper {
    @Insert("insert into item(name, price, description, category, user_id, create_time, update_time) " +
            "values (#{name}, #{price}, #{description}, #{category}, #{userId}, now(), now())")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    boolean addItem(Item item);

    @Select("select * from item where id = #{id}")
    @Results({
            @Result(property = "status", column = "status", javaType = ItemStatus.class),
            @Result(property = "category", column = "category", javaType = ItemCategory.class)
    })
    Item selectItemById(Long id);

    @Select("select user_id from item where id = #{id}")
    Long selectUserIdById(Long id);

    @Select("select status from item where id = #{id}")
    @Result(property = "status", column = "status", javaType = ItemStatus.class)
    ItemStatus selectStatusById(Long id);

    @Update("update item set name = #{name}, description = #{description}, category = #{category}, update_time = now() where id = #{id}")
    @Result(property = "category", column = "category", javaType = ItemCategory.class)
    boolean updateItem(Item item);

    @Update("update item set price = #{price}, update_time = now() where id = #{id}")
    boolean updatePriceById(Long id, BigDecimal price);

    @Update("update item set status = #{itemStatus}, update_time = now() where id = #{id}")
    boolean updateStatusById(Long id, ItemStatus itemStatus);

    @Delete("delete from item where id = #{id}")
    boolean deleteItemById(Long id);

    /**
     * 条件分页搜索商品
     * @param keyword 搜索关键词
     * @param category 商品类别
     * @param campus 商品所属校区
     * @param minPrice 最小价格
     * @param maxPrice 最大价格
     * @param sortByPrice 按价格排序方式
     * @return 带有商品数据的Page列表
     */
    Page<ItemListVO> searchItem(String keyword, ItemCategory category, Campus campus, BigDecimal minPrice, BigDecimal maxPrice, SortOrder sortByPrice);

    @Select("select id, name, price, status, (select url from item_image where item_id = item.id order by id limit 1) as image, create_time, update_time " +
            "from item where user_id = #{userId} order by id desc")
    @Result(property = "status", column = "status", javaType = ItemStatus.class)
    List<ItemListVO> getCurrentItemList(Long userId);

    @Select("select id, name, price, status, (select url from item_image where item_id = item.id order by id limit 1) as image, create_time, update_time " +
            "from item where user_id = #{userId} and (status='available' or status='trading' or status='sold') order by id desc ")
    @Result(property = "status", column = "status", javaType = ItemStatus.class)
    List<ItemListVO> selectItemListByUserId(Long userId);

    @Select("select id, name, price, status, (select url from item_image where item_id = item.id order by id limit 1) as image, create_time, update_time " +
            "from item where id = #{id}")
    @Result(property = "status", column = "status", javaType = ItemStatus.class)
    ItemListVO selectItemListVOById(Long id);

    @Select("select id, name, price, status, (select url from item_image where item_id = item.id order by id limit 1) as image, create_time, update_time " +
            "from item where status = 'pending' order by update_time asc")
    @Result(property = "status", column = "status", javaType = ItemStatus.class)
    Page<ItemListVO> selectPendingItemList();
}
